import { getDictMap } from '@/api/system/dictDetail'

// 对查询结果排序
const dictScort = data => {
  data.sort((val1, val2) => {
    return val1.sort - val2.sort
  })
}

const handleRes = (array, commit, name) => {
  const data = []
  dictScort(array)
  array.forEach(item => {
    let id = Number(item.value)
    if (isNaN(id)) {
      id = item.value
    }
    data.push({
      id,
      text: item.label
    })
  })
  commit(name, data)
}

const handleExpressRes = (array, commit, name) => {
  const data = []
  dictScort(array)
  array.forEach(item => {
    data.push({
      id: item.sort,
      text: item.label,
      prefix: item.value
    })
  })
  commit(name, data)
}

// 待缓存的字典 {key:value} => key-字典名称，value-state事件名称
const dicMap = {
  'delivery_type': 'SET_DELIVERY_TYPE',
  'wareHousing_type': 'SET_WARE_TYPE',
  'drug_type': 'SET_DRUG_TYPE',
  'work_unit': 'SET_WORKUNIT_TYPE',
  'nation': 'SET_NATION_TYPE',
  'marital_status': 'SET_MARITALSTATUS_TYPE',
  'account_source': 'SET_ACCOUNTSOURCE_TYPE',
  'process_cost': 'SET_PROCESSCOST',
  'package_unit': 'SET_PACKAGEUNIT',
  'dose_unit': 'SET_DOSEUNIT',
  'frequencys': 'SET_FREQUENCYS',
  'dection_name': 'SET_DECTIONNAME',
  'gf_process_cost': 'SET_GF_PROCESSCOST',
  'sizes': 'SET_PAGE_SIZES',
  'expressChannel': 'SET_EXPRESS',
  'nextRemarks': 'SET_NEXT_REMARKS',
  'reissueReason': 'SET_REISSUEREASONS'
}

const dict = {
  state: {
    deliveryType: [],
    wareHousingType: [],
    drugType: [],
    workunitsType: [],
    nationsType: [],
    maritalstatussType: [],
    accountsourcesType: [],
    processCost: 0, // 饮片加工费
    gfPocessCost: 0, // 膏方加工费
    packageUnit: [], // 包装单位
    doseUnit: [], // 剂量单位
    frequencys: [], // 用药频次
    dectionName: [], // 煎法
    sizes: [], // 分页个数
    expressChannel: [], // 物流公司
    nextRemarks: [], // 延期发货原因
    reissueReasons: [] // 补发原因
  },
  mutations: {
    SET_DELIVERY_TYPE: (state, value) => {
      state.deliveryType = value
    },
    SET_WARE_TYPE: (state, value) => {
      state.wareHousingType = value
    },
    SET_DRUG_TYPE: (state, value) => {
      state.drugType = value
    },
    SET_WORKUNIT_TYPE: (state, value) => {
      state.workunitsType = value
    },
    SET_NATION_TYPE: (state, value) => {
      state.nationsType = value
    },
    SET_MARITALSTATUS_TYPE: (state, value) => {
      state.maritalstatussType = value
    },
    SET_ACCOUNTSOURCE_TYPE: (state, value) => {
      state.accountsourcesType = value
    },
    SET_PROCESSCOST: (state, value) => {
      state.processCost = value[0].id
    },
    SET_PACKAGEUNIT: (state, value) => {
      state.packageUnit = value
    },
    SET_DOSEUNIT: (state, value) => {
      state.doseUnit = value
    },
    SET_FREQUENCYS: (state, value) => {
      state.frequencys = value
    },
    SET_DECTIONNAME: (state, value) => {
      state.dectionName = value
    },
    SET_GF_PROCESSCOST: (state, value) => {
      state.gfPocessCost = value[0].id
    },
    SET_PAGE_SIZES: (state, value) => {
      let sizes = []
      if (value && value.length) {
        value.forEach(item => {
          sizes.push(item.id)
        })
      } else {
        // 字典未配置？设置默认参数
        sizes = [10, 20, 30, 40, 50, 100]
      }
      state.sizes = sizes
    },
    SET_EXPRESS: (state, value) => {
      state.expressChannel = value
    },
    SET_NEXT_REMARKS: (state, value) => {
      state.nextRemarks = value
    },
    SET_REISSUEREASONS: (state, value) => {
      state.reissueReasons = value
    }
  },
  actions: {
    // 初始化字典
    intiDict({ commit }) {
      const list = []
      for (const key in dicMap) {
        list.push(key)
      }

      return new Promise((resolve, reject) => {
        getDictMap(list.join(',')).then(res => {
          for (const key in res) {
            if (key === 'expressChannel') {
              handleExpressRes(res[key], commit, dicMap[key])
            } else {
              handleRes(res[key], commit, dicMap[key])
            }
          }
          resolve()
        }).catch(error => {
          console.error(error)
          reject(error)
        })
      })
    }
  }
}

export default dict
